﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using System.Data;
using Besture.Bll;
using Besture.Dal;
using Besture.Model;
using Besture.Common;

namespace Besture.UI.Portal.Hotel
{
    public partial class Product : System.Web.UI.Page
    {
        public static string Img_Path = "/Resource/images/";

        public string hotelName = "";
        public string beginTime = "";
        public string endTime = "";
        
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request.QueryString["hn"] != null)
            {
                hotelName = Request.QueryString["hn"].ToString();
            }
            if (Request.QueryString["bt"] != null)
            {
                beginTime = Request.QueryString["bt"].ToString();
            }
            if (Request.QueryString["et"] != null)
            {
                endTime = Request.QueryString["et"].ToString();
            }
        }

        /// <summary>
        /// 加载酒店查询条件
        /// </summary>
        /// <returns></returns>
        [WebMethod]
        public static string GetHotelsCategory()
        {
            string json = "";
            StringBuilder sb = new StringBuilder();
            HotelsCategoryDac dac = new HotelsCategoryDac();
            IList<HotelsCategoryModel> list = dac.GetHotelsCategory();
            IList<HotelsCategoryModel> list1 = list.Where(p => p.CategoryType == 1).ToList();
            if (list1.Count > 0)
            {
                foreach (HotelsCategoryModel m in list1)
                {
                    sb.Append("{");
                    sb.Append("\"ID\":" + m.ID);
                    sb.Append(",");
                    sb.Append("\"CategoryName\":" + "\"" + m.CategoryName + "\"");
                    sb.Append("},");
                }
            }
            json += "{\"items\":[" + sb.ToString().Trim(',') + "]},";
            sb.Clear();
            IList<HotelsCategoryModel> list2 = list.Where(p => p.CategoryType == 2).ToList();
            if (list2.Count > 0)
            {
                foreach (HotelsCategoryModel m in list2)
                {
                    sb.Append("{");
                    sb.Append("\"ID\":" + m.ID);
                    sb.Append(",");
                    sb.Append("\"CategoryName\":" + "\"" + m.CategoryName + "\"");
                    sb.Append(",");
                    sb.Append("\"BeginPrice\":" + "\"" + m.BeginPrice.ToString() + "\"");
                    sb.Append(",");
                    sb.Append("\"EndPrice\":" + "\"" + m.EndPrice.ToString() + "\"");
                    sb.Append("},");
                }
            }
            json += "{\"items\":[" + sb.ToString().Trim(',') + "]},";
            sb.Clear();
            IList<HotelsCategoryModel> list3 = list.Where(p => p.CategoryType == 3).ToList();
            if (list3.Count > 0)
            {
                foreach (HotelsCategoryModel m in list3)
                {
                    sb.Append("{");
                    sb.Append("\"ID\":" + m.ID);
                    sb.Append(",");
                    sb.Append("\"CategoryName\":" + "\"" + m.CategoryName + "\"");
                    sb.Append("},");
                }
            }
            json += "{\"items\":[" + sb.ToString().Trim(',') + "]},";
            sb.Clear();
            IList<HotelsCategoryModel> list4 = list.Where(p => p.CategoryType == 4).ToList();
            if (list4.Count > 0)
            {
                foreach (HotelsCategoryModel m in list4)
                {
                    sb.Append("{");
                    sb.Append("\"ID\":" + m.ID);
                    sb.Append(",");
                    sb.Append("\"CategoryName\":" + "\"" + m.CategoryName + "\"");
                    sb.Append("},");
                }
            }
            json += "{\"items\":[" + sb.ToString().Trim(',') + "]},";
            sb.Clear();
            IList<HotelsCategoryModel> list5 = list.Where(p => p.CategoryType == 5 && p.ParentID == 0).ToList();
            if (list5.Count > 0)
            {
                foreach (HotelsCategoryModel m in list5)
                {
                    sb.Append("{");
                    sb.Append("\"ID\":" + m.ID);
                    sb.Append(",");
                    sb.Append("\"CategoryName\":" + "\"" + m.CategoryName + "\"");
                    sb.Append("},");
                }
            }
            json += "{\"items\":[" + sb.ToString().Trim(',') + "]},";
            sb.Clear();
            if (list5.Count > 0)
            {
                foreach (HotelsCategoryModel m in list5)
                {
                    IList<HotelsCategoryModel> listChild = list.Where(p => p.CategoryType == 5 && p.ParentID == m.ID).ToList();
                    if (listChild.Count > 0)
                    {
                        foreach (HotelsCategoryModel n in listChild)
                        {
                            sb.Append("{");
                            sb.Append("\"ID\":" + n.ID);
                            sb.Append(",");
                            sb.Append("\"CategoryName\":" + "\"" + n.CategoryName + "\"");
                            sb.Append("},");
                        }
                    }
                    json += "{\"items\":[" + sb.ToString().Trim(',') + "]},";
                    sb.Clear();
                }
            }
            sb.Clear();
            IList<HotelsCategoryModel> list6 = list.Where(p => p.CategoryType == 6).ToList();
            if (list6.Count > 0)
            {
                foreach (HotelsCategoryModel m in list6)
                {
                    sb.Append("{");
                    sb.Append("\"ID\":" + m.ID);
                    sb.Append(",");
                    sb.Append("\"CategoryName\":" + "\"" + m.CategoryName + "\"");
                    sb.Append("},");
                }
            }
            json += "{\"items\":[" + sb.ToString().Trim(',') + "]},";
            return "[" + json + "]";
        }

        /// <summary>
        /// 查询酒店列表
        /// </summary>
        /// <param name="filter"></param>
        /// <returns></returns>
        [WebMethod]
        public static string GetHotels(string filter, string pageindex)
        {
            string total = "";
            int userID = 0;
            int userType = 0;
            StringBuilder sb = new StringBuilder();
            HotelsDac dac = new HotelsDac();

            if (HttpContext.Current.Session[Constants.SESSION_WEB_USER_MODEL] != null)
            {
                UserBaseModel model = (UserBaseModel)HttpContext.Current.Session[Constants.SESSION_WEB_USER_MODEL];
                userID = model.ID;
                userType = model.UserType;
            }

            string[] arr = filter.Split('_');

            DataSet ds = dac.GetHotels(filter, pageindex,userID, userType, out total);
            IList<HotelList_Hotel_Model> listHotel = ModelConvertHelper<HotelList_Hotel_Model>.ConvertToModelList(ds.Tables[0]);
            IList<HotelList_HotelRoom_Model> listRoom = ModelConvertHelper<HotelList_HotelRoom_Model>.ConvertToModelList(ds.Tables[1]);
            IList<HotelList_RoomImage_Model> listImage = ModelConvertHelper<HotelList_RoomImage_Model>.ConvertToModelList(ds.Tables[2]);
            IList<HotelList_Fav_Model> listFav = ModelConvertHelper<HotelList_Fav_Model>.ConvertToModelList(ds.Tables[3]);

            foreach (HotelList_Hotel_Model m1 in listHotel)
            {
                sb.Append("<div class=\"item\">");
                sb.Append("<div class=\"hotel_info\">");
                sb.Append("<img alt=\"\" class=\"fleft\" style=\"width:179px; height:126px;\" src=\"" + Constants.PATH_IMAGE_CONTENT + m1.HotelsImage + "\" />");
                sb.Append("<div class=\"info fright\">");
                sb.Append("<div class=\"hotel_name\">");
                sb.Append("<a class=\"fleft\" href=\"javascript:Redirect(" + m1.HotelID + ")\">" + m1.HotelsName + "</a><span class=\"fright\"><b>" + m1.Score.ToString("f1").Replace(".0", "") + "</b>/5分</span>");
                sb.Append("<br class=\"clear\" /></div>");
                sb.Append("<p>地址：" + m1.Address + "</p>");
                sb.Append("<p>电话：" + m1.Tel + "</p>");
                sb.Append("<div class=\"hotel_price\">");
                sb.Append("<div class=\"fleft\"><sup>￥</sup><span>" + m1.LowPrice.ToString("f0") + "</span>起</div>");
                //sb.Append("<div class=\"fright\"><a href=\"javascript:ShowMap('" + m1.HotelsName + "/" + m1.HotelsImage + "/" + m1.Address + "/" + m1.Tel + "/" + "')\">查看地图</a>");
                sb.Append("<div class=\"fright\"><a href=\"javascript:ShowMap('" + m1.Address + "')\" addr=\"" + m1.Address + "\" hn=\"" + m1.HotelsName + "\">查看地图</a>");
                if (listFav.Where(p => p.HotelID == m1.HotelID).SingleOrDefault() != null)
                {
                    sb.Append("<a href=\"javascript:CollectHotels(" + m1.HotelID + ");\" tag=\"" + m1.HotelID + "\" style=\"background:url(../../Resource/images/fav2.png) left center no-repeat\">收藏</a></div><br class=\"clear\" /></div></div><br class=\"clear\" />");
                }
                else
                {
                    sb.Append("<a href=\"javascript:CollectHotels(" + m1.HotelID + ");\" tag=\"" + m1.HotelID + "\" style=\"background:url(../../Resource/images/fav.png) left center no-repeat\">收藏</a></div><br class=\"clear\" /></div></div><br class=\"clear\" />");
                }
                sb.Append("</div>");

                sb.Append("<table cellpadding=\"0\" cellspacing=\"0\" border=\"0\">");
                sb.Append("<tr><td>房型</td><td>床型</td><td>早餐</td><td>宽带</td><td>房价</td><td>操作</td></tr>");
                IList<HotelList_HotelRoom_Model> listRoom1 = listRoom.Where(p => p.HotelID == m1.HotelID).ToList();
                foreach (HotelList_HotelRoom_Model m2 in listRoom1)
                {
                    sb.Append("<tr>");
                    sb.Append("<td><img alt=\"\" style=\"width:50px; height:50px;\" src=\"" + Constants.PATH_IMAGE_CONTENT + m2.RoomImage + "\" /><br /><a href=\"javascript:;\" class=\"room_type\">" + m2.RoomName + "</a></td>");
                    sb.Append("<td>" + m2.BedType + "</td>");
                    sb.Append("<td>" + m2.Breakfast + "</td>");
                    sb.Append("<td>" + m2.Broadband + "</td>");
                    sb.Append("<td><p class=\"price\">￥<span>" + m2.RoomPrice.ToString("f0") + "</span></p></td>");
                    if (m2.HousingStateID == 3)
                    {
                        sb.Append("<td><a href=\"javascript:alert('房间已满！');\" class=\"btn disable\">预定</a> <span class=\"btn_tip\"><img alt=\"\" src=\"" + Img_Path + "tipico.png\" />" + m2.HousingState + "</span></td>");
                    }
                    else
                    {
                        sb.Append("<td><a href=\"javascript:window.location.href='../Pay/OrderInfo.aspx?ud=" + m1.HotelID.ToString() + "/" + m2.RoomID.ToString() + "/" + arr[1] + "/" + arr[2] + "'\" class=\"btn\">预定</a> <span class=\"btn_tip\"><img alt=\"\" src=\"" + Img_Path + "tipico.png\" />" + m2.HousingState + "</span></td>");
                    }
                    //m1.HotelID.ToString() + "/" + m2.RoomID.ToString() + "/" + arr[1] + "/" + arr[2]
                    sb.Append("</tr>");
                    sb.Append("<tr>");
                    sb.Append("<td colspan=\"6\"><ul>");
                    sb.Append("<li>");
                    IList<HotelList_RoomImage_Model> listImage1 = listImage.Where(p => p.RoomID == m2.RoomID).ToList();
                    foreach (HotelList_RoomImage_Model m3 in listImage1)
                    {
                        sb.Append("<img alt=\"\" style=\"width:75px; height:75px;\" src=\"" + Constants.PATH_IMAGE_CONTENT + m3.ImageUrl + "\" /> ");
                    }
                    sb.Append("</li>");
                    sb.Append("<li>建筑面积：" + m2.RoomArea.ToString() + "平方米</li>");
                    sb.Append("<li>楼层：" + m2.Floor.ToString() + "层</li>");
                    sb.Append("<li>床型：" + m2.BedType + "</li>");
                    sb.Append("<li>无窗无烟房：" + m2.Smoke + "</li>");
                    sb.Append("<li>最多入住人数：" + m2.PeopleNumber.ToString() + "人</li>");
                    sb.Append("</ul><a href=\"#\" class=\"slideup\">收起</a></td>");
                    sb.Append("</tr>");
                }
                sb.Append("</table>");

                if (listRoom1.Count > 2)
                {
                    sb.Append("<div class=\"show_all\"><a href=\"#\">展开全部房型</a></div></div>");
                }
                else
                {
                    sb.Append("<div class=\"show_all\"></div></div>");
                }
            }

            int num = Convert.ToInt32(total);
            int page = num % 3 == 0 ? num / 3 : num / 3 + 1;

            return page.ToString() + "," + total + "家," + sb.ToString();
        }

        /// <summary>
        /// 查询推荐酒店列表
        /// </summary>
        /// <param name="filter"></param>
        /// <returns></returns>
        [WebMethod]
        public static string GetHotelsRecommend()
        {
            int userID = 0;

            if (HttpContext.Current.Session[Constants.SESSION_WEB_USER_MODEL] != null)
            {
                UserBaseModel model = (UserBaseModel)HttpContext.Current.Session[Constants.SESSION_WEB_USER_MODEL];
                userID = model.ID;
            }

            StringBuilder sb = new StringBuilder();
            HotelsDac dac = new HotelsDac();
            DataSet ds = dac.GetHotelsRecommend(userID);

            IList<HotelsModel> listHotel = ModelConvertHelper<HotelsModel>.ConvertToModelList(ds.Tables[0]);
            IList<HotelList_Fav_Model> listFav = ModelConvertHelper<HotelList_Fav_Model>.ConvertToModelList(ds.Tables[1]);

            foreach (HotelsModel m in listHotel)
            {
                sb.Append("{");
                sb.Append("\"ID\":\"" + m.ID + "\",");
                sb.Append("\"HotelsName\":\"" + m.HotelsName + "\",");
                sb.Append("\"HotelsImage\":\"" + Constants.PATH_IMAGE_CONTENT + m.HotelsImage + "\",");
                sb.Append("\"Address\":\"" + m.Address + "\",");
                sb.Append("\"Score\":\"" + m.Score.ToString("f1").Replace(".0","") + "\",");
                if (listFav.Where(p => p.HotelID == m.ID).SingleOrDefault() != null)
                {
                    sb.Append("\"IsFav\":\"fav2.png\"");
                }
                else
                {
                    sb.Append("\"IsFav\":\"fav.png\"");
                }
                sb.Append("},");
            }

            return "{\"items\":[" + sb.ToString().Trim(',') + "]}";
        }

        /// <summary>
        /// 酒店收藏
        /// </summary>
        /// <returns></returns>
        [WebMethod]
        public static string CollectHotels(int hotelid, int fav)
        {
            if (HttpContext.Current.Session[Constants.SESSION_WEB_USER_MODEL] != null)
            {
                UserBaseModel ubm = ((UserBaseModel)HttpContext.Current.Session[Constants.SESSION_WEB_USER_MODEL]);
                CollectDac dac = new CollectDac();
                if (fav == 1)
                {
                    CollectModel model = new CollectModel();
                    model.HotelsID = hotelid;
                    model.UserID = ubm.ID;
                    model.IsDeleted = 0;
                    model.Created = ubm.ID;
                    model.CreateTime = DateTime.Now;
                    model.Updated = ubm.ID;
                    model.UpdateTime = DateTime.Now;
                    int result = dac.Add(model);
                    return result == -1 ? "-1" : "1";
                }
                else
                {
                    int result = dac.CollectHotelsCancel(hotelid, ubm.ID);
                    return result == -1 ? "-1" : "1";
                }
            }
            else
            {
                return "0";
            }
        }
    }
}